package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.bean.Product;
import org.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/product")
@Tag(name = "商品管理",description = "实现对商品的增删改查工作")
public class ProductController {
    public static final int PRODUCT_STATUS_ACTIVE= 1;
    public static final int PRODUCT_STATUS_INACTIVE= 0;
    @Autowired
    private ProductService productService;

    @PostMapping("/add")
    @Operation(summary = "添加商品",description = "添加商品")
    public ResponseEntity<ResponseResult> addProduct(@RequestParam("category_id") Integer category_id,
                                                    @RequestParam("product_name") String product_name,
                                                    @RequestPart("mainimgfile") MultipartFile mainImgFile,
                                                    @RequestPart("subimgfile") MultipartFile[] subImgFile,
                                                    @RequestParam("description") String description,
                                                    @RequestParam("price") Double price,
                                                    @RequestParam("original_price") Double original_price){
        Product product = new Product();
        product.setCategory_id(category_id);
        product.setProduct_name(product_name);
        product.setMainImgFile(mainImgFile);
        product.setSubImgFile(subImgFile);
        product.setDescription(description);
        product.setPrice(price);
        product.setOriginal_price(original_price);
        product.setProduct_status(PRODUCT_STATUS_ACTIVE);
        productService.addProduct(product);
        product.setMainImgFile(null);
        product.setSubImgFile(null);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(product).build());
    }

    @GetMapping("/special/{product_status}")
    @Operation(summary = "查询特价商品",description = "查询特价商品")
    public ResponseResult specialProducts(@PathVariable("product_status") Integer product_status){
        List<Product> products = productService.getSpecialProducts(product_status);
        return ResponseResult.builder().code(1).msg("查询成功").data(products).build();
    }

    @GetMapping("/all/{product_status}")
    @Operation(summary = "根据状态查询所有商品",description = "根据商品状态查询所有商品")
    public ResponseResult allProducts(@PathVariable("product_status") Integer product_status){
        List<Product> products = productService.getAllProducts(product_status);
        return ResponseResult.builder().code(1).msg("查询成功").data(products).build();
    }

    @GetMapping("/{product_id}")
    @Operation(summary = "根据ID查询商品",description = "根据商品ID查询商品")
    public ResponseResult getProductById(@PathVariable("product_id") Integer product_id){
        Product product = productService.getProductById(product_id);
        return ResponseResult.builder().code(1).msg("查询成功").data(product).build();
    }

    @GetMapping("/category/{category_id}")
    @Operation(summary = "根据分类ID查询商品",description = "根据分类ID查询商品")
    public ResponseResult getProductsByCategoryId(@PathVariable("category_id") Integer category_id){
        List<Product> products = productService.getProductsByCategoryId(category_id);
        return ResponseResult.builder().code(1).msg("查询成功").data(products).build();
    }

    //根据产品id修改产品状态
    @PutMapping("/status/{product_id}/{product_status}")
    @Operation(summary = "根据产品ID修改产品状态",description = "根据产品ID修改产品状态")
    public ResponseResult updateProductStatus(@PathVariable("product_id") Integer product_id,
                                              @PathVariable("product_status") Integer product_status){
        boolean result = productService.updateProductStatus(product_id, product_status);
        return ResponseResult.builder().code(1).msg("修改成功").data(result).build();
    }

    //根据产品id修改产品信息
    @PutMapping("/editProduct/{product_id}")
    @Operation(summary = "根据产品ID修改产品信息",description = "根据产品ID修改产品信息")
    public ResponseResult updateProduct(@PathVariable("product_id") Integer product_id,
                                        @RequestParam("category_id") Integer category_id,
                                        @RequestParam("product_name") String product_name,
                                        @RequestPart("mainimgfile") MultipartFile mainImgFile,
                                        @RequestPart("subimgfile") MultipartFile[] subImgFile,
                                        @RequestParam("description") String description,
                                        @RequestParam("price") Double price,
                                        @RequestParam("original_price") Double original_price){
        Product product = new Product();
        product.setCategory_id(category_id);
        product.setProduct_name(product_name);
        product.setMainImgFile(mainImgFile);
        product.setSubImgFile(subImgFile);
        product.setDescription(description);
        product.setPrice(price);
        product.setOriginal_price(original_price);
        boolean result = productService.updateProduct(product_id, product);
        return ResponseResult.builder().code(1).msg("修改成功").data(result).build();
    }

    //根据产品id连接查询+类别表 查询全部信息
    @GetMapping("/detail/{product_id}")
    @Operation(summary = "根据产品ID查询商品详情",description = "根据产品ID查询商品详情")
    public ResponseResult getProductDetailById(@PathVariable("product_id") Integer product_id){
        Product product = productService.getProductDetailById(product_id);
        return ResponseResult.builder().code(1).msg("查询成功").data(product).build();
    }
}
